package com.translate.web.service.impl;

import com.translate.web.config.contract.IOSSConfig;
import com.translate.web.constant.RedisConstant;
import com.translate.web.dao.TranslateMapper;
import com.translate.web.param.contract.IIdAndUserIdParam;
import com.translate.entity.Translate;
import com.translate.vo.contract.ITranslateVO;
import com.translate.web.service.ITranslateService;
import javax.annotation.Resource;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;


@Service
public class TranslateServiceImpl extends BaseTranslateServiceImpl<Translate> implements ITranslateService {
	private IOSSConfig config;
	
	
	
	public TranslateServiceImpl(TranslateMapper mapper) {
		super(mapper);
	}

	protected void insertPropteris(Translate data) {
		long time = System.currentTimeMillis() / 1000;

		data.setAuditStatus((byte) 0);
		
		data.setApplyTime(time);
		data.setCreateTime(time);
		data.setUpdateTime(time);
	}

	protected void updatePropteris(Translate data) {
		data.setUpdateTime(System.currentTimeMillis() / 1000);
	}

	protected Class<Translate> getEntityClass() {
		return Translate.class;
	}
	
	protected Translate  getEntityInstance() {
		return new Translate();
	}
	
	@Resource
	public void setRedisTemplate(RedisTemplate<String, ? extends Object> redisTemplate) {
		this.redisTemplate = redisTemplate;
	}

	@Override
	public <VO extends ITranslateVO> VO getTranslate(IIdAndUserIdParam params, Class<VO> type) {
		String translateKey = RedisConstant.getTranslateKey(params.getId(), params.getUserId());
		
		VO dataByIdFromCache = this.getDataByIdFromCache(params, type, translateKey, 6);
		
		if (null == dataByIdFromCache.getId()) {
			return null;
		}
		
		String file = dataByIdFromCache.getFile();
		if (null != file) {
			dataByIdFromCache.setFile((this.config.getURL().concat(file)));
		}
		
		return dataByIdFromCache;
	}

	@Override
	public Translate getTranslate(Translate translate) {
		
		TranslateMapper mapper = this.getBaseMapper();
		
		return mapper.getTranslate(translate);
	}

	@Resource
	public void setConfig(IOSSConfig config) {
		this.config = config;
	}
}